package stv5;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;

/**
 * This class represents a single series that is being tracked.
 * 
 * id = the id of the series in our db (NOT tversity)
 * name = The name of the series
 * guid = The guid of the series, as assigned by Tversity, for example
 * lastPlay = the last episode number viewed
 * lastView = The date (in seconds) of last view for this series
 * @author Daniel
 *
 */
public class Series implements Comparable<Series> {
	private int id, lastPlay, lastView;
	private String name, guid;
	private String localPath = null;
	
	/**
	 * If true, this means that the series is DEFINITELY
	 * 	managed by us, and is not a "potential" one found
	 * 	by spidering the episode manager
	 */
	private boolean managed = true; 
	
	public Series(int id, String name, String guid, int lastPlay,
			int lastView) {
		this.id = id;
		this.name = name;
		this.guid = guid;
		this.lastPlay = lastPlay;
		this.lastView = lastView;
	}

	public Series(String name, String guid) {
		this( -1, name, guid, -1, -1);
		managed = false;
	}

	@Override
	public int compareTo(Series other) {		
		//return getId() - other.getId();
		return getName().compareTo(other.getName());
	}

	public String formatUpdate() {
		return "UPDATE `series` SET `lastplay`='"+getLastPlay()+"',`lastview`='"
			+getLastView()+"' WHERE `id`='"+getId()+"' LIMIT 1";
	}

	/**
	 * @return the guid
	 */
	public String getGuid() {
		return guid;
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @return the last-played episode number
	 */
	public int getLastPlay() {
		return lastPlay;
	}
	
	/**
	 * @return the lastView
	 */
	public int getLastView() {
		return lastView;
	}

	public String getLocalPath() {
		return localPath;
	}
	
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return A url to load the next episode in this series
	 */
	public String getNextLink() {
		return "/media/view/?id="+getId()+"&amp;ep="+(getLastPlay()+1)+"&amp;save=1";
	}
	
	public boolean isManaged() {
		return managed;
	}
	
	/**
	 * I couldn't think of a better name, but this method
	 * 	sets this series to "managed" and loads the relevant
	 * 	data from the other series.
	 * 
	 * If the two Series have different names, this does nothing!
	 * @param other
	 */
	public void manageIfy(Series other) {
		if (name.equalsIgnoreCase(other.getName())) {
			id = other.getId();
			lastPlay = other.getLastPlay();
			lastView = other.getLastView();
			managed = true;
		}
	}

	public void setGuid(String guid) {
		this.guid = guid;	
	}
	
	/**
	 * @param lastPlay the lastPlay to set
	 */
	public void setLastPlay(int lastPlay) {
		this.lastPlay = lastPlay;
	}
	
	/**
	 * @param lastView the lastView to set
	 */
	public void setLastView(int lastView) {
		this.lastView = lastView;
	}

	public void setLocalPath(String localPath) {
		this.localPath = localPath;
	}

	public void updateLastView() {
		Calendar now = Calendar.getInstance();
		setLastView(Integer.parseInt(""+now.getTimeInMillis()/1000));
	}

	public static Series fromDB(ResultSet rs) throws SQLException {
		int id = rs.getInt("id");
		String name = rs.getString("name");
		String guid = rs.getString("guid");
		int lastPlay = rs.getInt("lastplay");
		int lastView = rs.getInt("lastview");
		
		return new Series(id, name, guid, lastPlay, lastView);
	}
}
